package com.jzqf.ndk;

import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.jzqf.ndk.databinding.ActivityMainBinding;
import com.jzqf.ndk.jni.Ndk;

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
    }

    @Override
    protected void onStart() {
        super.onStart();
        Ndk ndk = new Ndk();
        binding.javaBtn.setOnClickListener(v -> {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("getTypeString=").append(ndk.getTypeString("android")).append("\n");
            stringBuilder.append("getTypeBoolean=").append(ndk.getTypeBoolean(true)).append("\n");
            stringBuilder.append("getTypeChar=").append(ndk.getTypeChar('a')).append("\n");
            stringBuilder.append("getTypeDouble=").append(ndk.getTypeDouble(1.0)).append("\n");
            stringBuilder.append("getTypeInt=").append(ndk.getTypeInt(2)).append("\n");
            stringBuilder.append("getTypeFloat=").append(ndk.getTypeFloat(3.0f)).append("\n");
            stringBuilder.append("getTypeShort=").append(ndk.getTypeShort((short) 4)).append("\n");
            stringBuilder.append("getTypeLong=").append(ndk.getTypeLong(System.currentTimeMillis())).append("\n");
            binding.sampleText.setText(stringBuilder.toString());
        });
        binding.uninstallBtn.setOnClickListener(v -> {
            uninstall();
        });
    }

    private void startApplication() {
        Intent intent = new Intent();
//        PackageManager packageManager = getPackageManager();
//        intent = packageManager.getLaunchIntentForPackage("com.android.launcher3");
        ComponentName componentName = new ComponentName("com.android.launcher3",
                "com.android.launcher3.Launcher");
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        intent.setComponent(componentName);
        startActivity(intent);
    }

    private final int REQUEST_CODE = 101;

    private void uninstall() {
        //            intent.setAction(Intent.ACTION_DELETE);
//        Uri aPackage = Uri.fromParts("package", "com.jzqf.smart", null);
        Intent intent = new Intent(Intent.ACTION_DELETE);
        intent.setData(Uri.parse("package:com.io.iodevface"));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivityForResult(intent, REQUEST_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                Toast.makeText(this, "点击了确定", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "点击了取消"+resultCode, Toast.LENGTH_SHORT).show();
            }
        }
    }
}